import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { IEmployee } from '../model/interfaces'
import { employeesApi } from '../api/employeesApi'

interface IEmployeesState {
  employees: IEmployee[]
}

const initialState: IEmployeesState = {
  employees: []
}

export const employeesSlice = createSlice({
  name: 'employees',
  initialState,
  reducers: {
    setEmployees(state, {payload}: PayloadAction<IEmployee[]>) {
      state.employees = payload
    }
  },
  extraReducers: (builder) => {
    builder.addMatcher(employeesApi.endpoints?.getEmployees.matchFulfilled, (state, {payload}: PayloadAction<IEmployee[]>) => {
      state.employees = payload
    })
  }
})

export const employeesActions = employeesSlice.actions
export const employeesReducer = employeesSlice.reducer
